package com.example.springfeature.webmvc;

import com.example.springfeature.postProcessor.User;
import org.apache.catalina.manager.util.SessionUtils;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.servlet.http.HttpSession;

/**
 * Created with IDEA
 *
 * @author: 董文辉
 * Date: 2019/2/22
 * Time: 10:59
 */
@RestController
public class DefaultController {

    @RequestMapping("/exampleGet")
    String exampleGet(){
        String sessionKey = "session";
        HttpSession session = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest().getSession(true);
        Object user = session.getAttribute("user");
        if(user == null){
            System.out.println("第一次将对象放进session中");
            User temp = new User();
            temp.setName("sessionUser");
            temp.setPhone("sessionUserPhone");
            session.setAttribute("user",temp);
        }
        User user1 = (User) session.getAttribute("user");
        return user1.toString();
    }
}
